//
//  GHNetWorkTools.m
//  guahao
//
//  Created by osx on 16/9/24.
//  Copyright © 2016年 osx. All rights reserved.
//

#import "GHNetWorkTools.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"


static GHNetWorkTools *_instance;
@implementation GHNetWorkTools

//static GHNetWorkTools * tools;
+ (instancetype)sharedTools{
    //NSString *urlstr = @"http://172.27.35.1:8080/guahao/servlet/";
    NSString *urlstr = @"http://172.18.26.167:8080/guahao/servlet/";
    NSURL *url = [NSURL URLWithString:urlstr];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[GHNetWorkTools alloc] initWithBaseURL:url];
    });
    
    // self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
    
    _instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"charset=utf-8", nil];
    
    //设置请求的数据类型为普通的二进制类型.
    _instance.requestSerializer = [AFJSONRequestSerializer serializer];
    
    _instance.responseSerializer = [AFHTTPResponseSerializer serializer];

    return _instance;
}

- (void)requestDataWith:(MethodName)methodName urlString:(NSString *)urlString parameters:(NSDictionary *)parameters finishedBlock:(FinishedBlock)finished{
    
    
    if (methodName == POST) {
        
        
        [_instance POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            id result = nil;
            
            if ([responseObject isKindOfClass:[NSData class]]){
                
                result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
            }else{
                
                result = responseObject;
            }
            
            finished(result,nil);
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            finished(nil,error);
            
        }];
    }else{
        
        
        [_instance GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            id result = nil;
            
            if ([responseObject isKindOfClass:[NSData class]]){
                
                result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
            }else{
                
                result = responseObject;
            }
            
            finished(result,nil);
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            finished(nil,error);
            [SVProgressHUD showErrorWithStatus:@"网络请求失败！"];
            
        }];
        
        
        
    }
    
}




@end
